package globaldata

import (
	"errors"
	"net"
	"sync"

	"github.com/ThingsPanel/tp-protocol-sdk-go/api"
)

type DeviceConfig struct {
	AccessToken    string `json:"AccessToken"`
	CtrlWord       string `json:"Ctrl_Word"`
	DeviceId       string `json:"DeviceId"`
	IPPORT         string `json:"IP_PORT"`
	PictureTime    string `json:"Picture_time"`
	SampleTime     string `json:"Sample_time"`
	SendTime       string `json:"Send_time"`
	SubDeviceAddr  string `json:"SubDeviceAddr"`
	ThresholdLower string `json:"Threshold_lower"`
	ThresholdUpper string `json:"Threshold_upper"`
	SetHistory     string `json:"setHistory"`
	WarnMode       string `json:"warnMode"`
	Wave           string `json:"wave"`
}

// 平台网关配置map, key是网关的token，value是网关的配置
var DeviceConfigMap = make(map[string]*api.DeviceConfigResponseData)

// 设备连接map, key是设备的token，value是设备的连接
var DeviceConnectionMap = make(map[string]*net.Conn)

// 网关互斥锁map, key是网关的token，value是网关的互斥锁
var DeviceMutexMap = make(map[string]*sync.Mutex)

// 获取锁
func GetMutex(token string) error {
	if mutex, exists := DeviceMutexMap[token]; exists {
		mutex.Lock()
		return nil
	} else {
		return errors.New("锁不存在")
	}
}

// 释放锁
func ReleaseMutex(token string) error {
	if mutex, exists := DeviceMutexMap[token]; exists {
		mutex.Unlock()
		return nil
	} else {
		return errors.New("锁不存在")
	}
}
